/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package quizzempire.view;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import javax.swing.DefaultListModel;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import quizzempire.model.IModel;
import quizzempire.model.IQuestion;
import quizzempire.model.IQuizz;
import quizzempire.model.statemachine.State;

/**
 *
 * @author Victor
 */
public class QuestionEditor extends javax.swing.JFrame implements Observer {

    /**
     * Creates new form QuestionEditor
     */
    
    IModel model;
    IQuizz quizz;
    ArrayList<IQuestion> questions;
    DefaultListModel listModel = new DefaultListModel();
    JCheckBox cbDif[] = new JCheckBox[3];
    JRadioButton rbDif[] = new JRadioButton[3];
    JTextField answers[] = new JTextField[4];
    JRadioButton rbCorrect[] = new JRadioButton[4];
    int selectedid = 0;
    int selectedQuizzId;
    boolean saved = false, updating = false;
    
    public QuestionEditor(IModel model) {
        initComponents();
        Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
        this.setLocation(dim.width/2-this.getSize().width/2, dim.height/2-this.getSize().height/2);
        this.model = model;
        quizz = model.getQuizzViewer();
        questions = quizz.getQuestionsViewer();
        lbLogo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/quizzempire/resources/gamerlogo.png")));
        cbDif[0] = cbEasy;
        cbDif[1] = cbMedium;
        cbDif[2] = cbHard;
        
        rbDif[0] = rbEasy;
        rbDif[1] = rbMedium;
        rbDif[2] = rbHard;
        
        answers[0] = textAnswerA;
        answers[1] = textAnswerB;
        answers[2] = textAnswerC;
        answers[3] = textAnswerD;
        
        rbCorrect[0] = rbAnswerA;
        rbCorrect[1] = rbAnswerB;
        rbCorrect[2] = rbAnswerC;
        rbCorrect[3] = rbAnswerD;
                
        initFields();
        
        if (QuestionList.getSelectedIndex() == -1)
            setQuestionComponents(false);
        else
            setQuestionComponents(true);
        
        cbEasy.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (!verificaQuestion())
                {
                    cbEasy.setSelected(!cbEasy.isSelected());
                    return;
                }
                updateQuestionList();
            }
        }); 
        
        cbMedium.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (!verificaQuestion())
                {
                    cbMedium.setSelected(!cbMedium.isSelected());
                    return;
                }
                updateQuestionList();
            }
        });
        
        cbHard.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (!verificaQuestion())
                {
                    cbHard.setSelected(!cbHard.isSelected());
                    return;
                }
                updateQuestionList();
            }
        }); 
        
        
        QuestionList.addListSelectionListener(new ListSelectionListener()
        {

            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting())
                    return;
                
                if (!verificaQuestion())
                {
                    QuestionList.removeListSelectionListener(this);
                    QuestionList.setSelectedIndex(e.getLastIndex());
                    QuestionList.addListSelectionListener(this);
                    return;
                }
                

                selectedid = QuestionList.getSelectedIndex();

                ComboItem item = (ComboItem)QuestionList.getSelectedValue();
                if (item != null)
                    selectedQuizzId = item.id;
                else
                    selectedQuizzId = -1;

                if (!updating)
                    updateActualQuestion();

                if (QuestionList.getSelectedIndex() == -1)
                    setQuestionComponents(false);
                else
                    setQuestionComponents(true);
            }

        });
        
        model.addObserver(this);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        groupCorrectAnswer = new javax.swing.ButtonGroup();
        rbEasy = new javax.swing.JRadioButton();
        rbMedium = new javax.swing.JRadioButton();
        rbHard = new javax.swing.JRadioButton();
        lbLogo = new javax.swing.JLabel();
        lbDifficulty = new javax.swing.JLabel();
        cbEasy = new javax.swing.JCheckBox();
        cbMedium = new javax.swing.JCheckBox();
        cbHard = new javax.swing.JCheckBox();
        listQuestions = new javax.swing.JScrollPane();
        QuestionList = new javax.swing.JList();
        btMainMenu = new javax.swing.JButton();
        btPreviousStep = new javax.swing.JButton();
        buttonSave = new javax.swing.JButton();
        rbAnswerA = new javax.swing.JRadioButton();
        rbAnswerB = new javax.swing.JRadioButton();
        rbAnswerC = new javax.swing.JRadioButton();
        rbAnswerD = new javax.swing.JRadioButton();
        tbNewQuestion = new javax.swing.JTextField();
        btNewQuestion = new javax.swing.JButton();
        lbCounterEasy = new javax.swing.JLabel();
        lbCounterMedium = new javax.swing.JLabel();
        lbCounterHard = new javax.swing.JLabel();
        lbQuestion = new javax.swing.JLabel();
        textQuestion = new javax.swing.JScrollPane();
        taQuestion = new javax.swing.JTextArea();
        lbAnswers = new javax.swing.JLabel();
        textAnswerA = new javax.swing.JTextField();
        textAnswerC = new javax.swing.JTextField();
        textAnswerB = new javax.swing.JTextField();
        textAnswerD = new javax.swing.JTextField();
        lbFilter = new javax.swing.JLabel();
        buttonRemove = new javax.swing.JButton();
        saveQuestion = new javax.swing.JButton();
        resetQuestion = new javax.swing.JButton();
        lbQuestion1 = new javax.swing.JLabel();
        tbName = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        buttonGroup1.add(rbEasy);
        rbEasy.setText("Easy");
        rbEasy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbEasyActionPerformed(evt);
            }
        });

        buttonGroup1.add(rbMedium);
        rbMedium.setText("Medium");

        buttonGroup1.add(rbHard);
        rbHard.setText("Hard");

        lbLogo.setText("LOGO");

        lbDifficulty.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lbDifficulty.setText("Difficulty");

        cbEasy.setFont(new java.awt.Font("Dialog", 0, 10)); // NOI18N
        cbEasy.setText("Easy");

        cbMedium.setFont(new java.awt.Font("Dialog", 0, 10)); // NOI18N
        cbMedium.setText("Medium");

        cbHard.setFont(new java.awt.Font("Dialog", 0, 10)); // NOI18N
        cbHard.setText("Hard");

        QuestionList.setModel(listModel);
        QuestionList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        QuestionList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                QuestionListValueChanged(evt);
            }
        });
        listQuestions.setViewportView(QuestionList);

        btMainMenu.setText("MAIN MENU");
        btMainMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btMainMenuActionPerformed(evt);
            }
        });

        btPreviousStep.setText("PREVIOUS STEP");
        btPreviousStep.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPreviousStepActionPerformed(evt);
            }
        });

        buttonSave.setText("SAVE QUIZZ");
        buttonSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonSaveActionPerformed(evt);
            }
        });

        groupCorrectAnswer.add(rbAnswerA);
        rbAnswerA.setText("a)");
        rbAnswerA.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                rbAnswerAActionPerformed(evt);
            }
        });

        groupCorrectAnswer.add(rbAnswerB);
        rbAnswerB.setText("b)");

        groupCorrectAnswer.add(rbAnswerC);
        rbAnswerC.setText("c)");

        groupCorrectAnswer.add(rbAnswerD);
        rbAnswerD.setText("d)");

        btNewQuestion.setFont(new java.awt.Font("Dialog", 1, 10)); // NOI18N
        btNewQuestion.setText("NEW");
        btNewQuestion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNewQuestionActionPerformed(evt);
            }
        });

        lbCounterEasy.setFont(new java.awt.Font("Dialog", 0, 11)); // NOI18N
        lbCounterEasy.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbCounterEasy.setText("10/10");

        lbCounterMedium.setFont(new java.awt.Font("Dialog", 0, 11)); // NOI18N
        lbCounterMedium.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbCounterMedium.setText("5/10");

        lbCounterHard.setFont(new java.awt.Font("Dialog", 0, 11)); // NOI18N
        lbCounterHard.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbCounterHard.setText("10/30");

        lbQuestion.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lbQuestion.setText("Question");

        taQuestion.setColumns(20);
        taQuestion.setLineWrap(true);
        taQuestion.setRows(5);
        textQuestion.setViewportView(taQuestion);

        lbAnswers.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lbAnswers.setText("Answers");

        textAnswerC.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                textAnswerCActionPerformed(evt);
            }
        });

        lbFilter.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lbFilter.setText("Filter");

        buttonRemove.setText("Remove Question");
        buttonRemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonRemoveActionPerformed(evt);
            }
        });

        saveQuestion.setText("Save Question");
        saveQuestion.setToolTipText("");
        saveQuestion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveQuestionActionPerformed(evt);
            }
        });

        resetQuestion.setText("Reset Question");
        resetQuestion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetQuestionActionPerformed(evt);
            }
        });

        lbQuestion1.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lbQuestion1.setText("Name");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbLogo, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbFilter)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(listQuestions, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(tbNewQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(btNewQuestion)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cbEasy, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbMedium)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cbHard)))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(textQuestion)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(rbAnswerC)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(textAnswerC, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(rbAnswerD))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(rbAnswerA)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(textAnswerA, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(rbAnswerB)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(textAnswerB)
                                    .addComponent(textAnswerD, javax.swing.GroupLayout.Alignment.TRAILING)))
                            .addComponent(tbName)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(lbDifficulty)
                                    .addComponent(lbQuestion)
                                    .addComponent(lbAnswers)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(rbEasy, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(lbQuestion1)
                                            .addComponent(lbCounterEasy, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(rbMedium)
                                            .addComponent(lbCounterMedium, javax.swing.GroupLayout.PREFERRED_SIZE, 51, javax.swing.GroupLayout.PREFERRED_SIZE))
                                        .addGap(20, 20, 20)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(rbHard)
                                            .addComponent(lbCounterHard, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(resetQuestion)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(buttonRemove)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(saveQuestion, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btMainMenu)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btPreviousStep)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonSave, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lbLogo, javax.swing.GroupLayout.PREFERRED_SIZE, 120, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(25, 25, 25))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lbDifficulty)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lbCounterEasy)
                            .addComponent(lbCounterMedium)
                            .addComponent(lbCounterHard))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(rbEasy)
                            .addComponent(rbMedium)
                            .addComponent(rbHard))
                        .addGap(18, 18, 18)
                        .addComponent(lbQuestion1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(tbName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbQuestion)
                    .addComponent(lbFilter))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cbEasy)
                            .addComponent(cbMedium)
                            .addComponent(cbHard))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(listQuestions, javax.swing.GroupLayout.PREFERRED_SIZE, 158, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(textQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(lbAnswers)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(rbAnswerA)
                            .addComponent(textAnswerA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(rbAnswerB)
                            .addComponent(textAnswerB, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(rbAnswerC)
                            .addComponent(textAnswerC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(rbAnswerD)
                            .addComponent(textAnswerD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btNewQuestion)
                    .addComponent(tbNewQuestion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(resetQuestion)
                    .addComponent(buttonRemove)
                    .addComponent(saveQuestion))
                .addGap(0, 26, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btMainMenu)
                    .addComponent(btPreviousStep)
                    .addComponent(buttonSave))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void rbEasyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbEasyActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rbEasyActionPerformed

    private void rbAnswerAActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_rbAnswerAActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_rbAnswerAActionPerformed

    private void textAnswerCActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_textAnswerCActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_textAnswerCActionPerformed

    private void btMainMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btMainMenuActionPerformed
        model.restart();
    }//GEN-LAST:event_btMainMenuActionPerformed

    private void btNewQuestionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNewQuestionActionPerformed
        
        if (!verificaQuestion())
            return;
        
        if (tbNewQuestion.getText().isEmpty())
        {
            JOptionPane.showMessageDialog(this, "Question must have a name", "Question without name", JOptionPane.ERROR_MESSAGE);
            return;
        }
        selectedid = listModel.getSize();
        model.addQuestion(tbNewQuestion.getText());
        tbNewQuestion.setText("");
    }//GEN-LAST:event_btNewQuestionActionPerformed

    private void QuestionListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_QuestionListValueChanged
        
        
        
    }//GEN-LAST:event_QuestionListValueChanged

    private void btPreviousStepActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPreviousStepActionPerformed
        model.back();
    }//GEN-LAST:event_btPreviousStepActionPerformed

    private void buttonRemoveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonRemoveActionPerformed
        if(questions.size() == 0 || selectedQuizzId < 0 || selectedQuizzId >= questions.size())
            return;
        model.removeQuestion(selectedQuizzId);
    }//GEN-LAST:event_buttonRemoveActionPerformed

    private void saveQuestionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveQuestionActionPerformed
        
        if (!verificaQuestionComponents())
            return;
        
        int dif = 0, correct = 0;
        
        for (int i = 0; i < 3; i++)
        {
            if (rbDif[i].isSelected())
            {
                dif = i;
                break;
            }
        }
        
        for (int i = 0; i < 4; i++)
        {
            if(rbCorrect[i].isSelected())
            {
                correct = i;
                break;
            }
        }
        model.setQuestionDifficulty(selectedQuizzId, dif);
        model.setQuestionText(selectedQuizzId,taQuestion.getText());
        String a[] = {textAnswerA.getText(), textAnswerB.getText(), textAnswerC.getText(), textAnswerD.getText()};
        model.setQuestionAnswers(selectedQuizzId, a); 
        model.setIndexCorrect(selectedQuizzId, correct);
        model.setQuestionName(selectedQuizzId, tbName.getText());   
    }//GEN-LAST:event_saveQuestionActionPerformed

    private void resetQuestionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetQuestionActionPerformed
        updateActualQuestion();
    }//GEN-LAST:event_resetQuestionActionPerformed

    private void buttonSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonSaveActionPerformed
        if (!verificaQuestion())
            return;
        int [] contagem = {0, 0, 0};
        for(IQuestion x : questions)        
            contagem[x.getDifficulty()]++;
        boolean[] dif = quizz.getDifAvailable();
        int [] nQperLevel = quizz.getNumQuestionsLevel();
        
        if (dif[0] && contagem[0] < nQperLevel[0])
        {
            JOptionPane.showMessageDialog(this, "Number of Easy Questions must reach "+nQperLevel[0], "Number of Easy Questions unreached", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        if (dif[1] && contagem[1] < nQperLevel[1])
        {
            JOptionPane.showMessageDialog(this, "Number of Medium Questions must reach "+nQperLevel[1], "Number of Medium Questions unreached", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        if (dif[2] && contagem[2] < nQperLevel[2])
        {
            JOptionPane.showMessageDialog(this, "Number of Hard Questions must reach "+nQperLevel[2], "Number of Hard Questions unreached", JOptionPane.ERROR_MESSAGE);
            return;
        }
        
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new File(model.getQuizzesFolder()));
        int retrival = chooser.showSaveDialog(this);
        if (retrival == JFileChooser.APPROVE_OPTION) {
            model.saveQuizz(chooser.getSelectedFile()+".qzz");
            saved = true;
        }
    }//GEN-LAST:event_buttonSaveActionPerformed



    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList QuestionList;
    private javax.swing.JButton btMainMenu;
    private javax.swing.JButton btNewQuestion;
    private javax.swing.JButton btPreviousStep;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton buttonRemove;
    private javax.swing.JButton buttonSave;
    private javax.swing.JCheckBox cbEasy;
    private javax.swing.JCheckBox cbHard;
    private javax.swing.JCheckBox cbMedium;
    private javax.swing.ButtonGroup groupCorrectAnswer;
    private javax.swing.JLabel lbAnswers;
    private javax.swing.JLabel lbCounterEasy;
    private javax.swing.JLabel lbCounterHard;
    private javax.swing.JLabel lbCounterMedium;
    private javax.swing.JLabel lbDifficulty;
    private javax.swing.JLabel lbFilter;
    private javax.swing.JLabel lbLogo;
    private javax.swing.JLabel lbQuestion;
    private javax.swing.JLabel lbQuestion1;
    private javax.swing.JScrollPane listQuestions;
    private javax.swing.JRadioButton rbAnswerA;
    private javax.swing.JRadioButton rbAnswerB;
    private javax.swing.JRadioButton rbAnswerC;
    private javax.swing.JRadioButton rbAnswerD;
    private javax.swing.JRadioButton rbEasy;
    private javax.swing.JRadioButton rbHard;
    private javax.swing.JRadioButton rbMedium;
    private javax.swing.JButton resetQuestion;
    private javax.swing.JButton saveQuestion;
    private javax.swing.JTextArea taQuestion;
    private javax.swing.JTextField tbName;
    private javax.swing.JTextField tbNewQuestion;
    private javax.swing.JTextField textAnswerA;
    private javax.swing.JTextField textAnswerB;
    private javax.swing.JTextField textAnswerC;
    private javax.swing.JTextField textAnswerD;
    private javax.swing.JScrollPane textQuestion;
    // End of variables declaration//GEN-END:variables

    @Override
    public void update(Observable o, Object arg) {
        State state = model.getState(); 
        if (!(state instanceof quizzempire.model.statemachine.QuestionEditor))
        {
            model.deleteObserver(this);
            MainBuilder.changeScreen(this, model, state);
        }
        else
        {
            updating = true;
            if (saved)
            {
                saved = false;
                JOptionPane.showMessageDialog(this, "Unexpected error a timeout occurred during the write operation", "Error during write operation", JOptionPane.ERROR_MESSAGE);
            }
            selectedQuizzId = -1;
            updateQuestionList();
            updateActualQuestion();
            updating = false;
        }
    }

    private void initFields() {
        
        cbEasy.setSelected(true);
        cbMedium.setSelected(true);
        cbHard.setSelected(true);
        
        updateQuestionList();
        updateActualQuestion();
    }


    
    class ComboItem
    {
        int id;
        String text;

        public ComboItem(int id, String text) {
            this.id = id;
            this.text = text;
        }

        @Override
        public String toString() {
            return text;
        }
        
    }
    
    private void updateQuestionList()
    {
        int aux = selectedid;
        listModel.clear();
        questions = quizz.getQuestionsViewer();
        int[] totalQuestions = quizz.getNumQuestionsLevel();
        int[] nQ = {0, 0, 0};
        for (int i = 0; i < questions.size();i++)
        {
            IQuestion x = questions.get(i); 
            nQ[x.getDifficulty()]++;
            if(cbDif[x.getDifficulty()].isSelected())
            {
                ComboItem item = new ComboItem(i, cbDif[x.getDifficulty()].getText().charAt(0)+" - "+x.getName());
                listModel.addElement(item);
            }
        }
              
        if (aux >= 0 && aux < listModel.getSize())
        {
            QuestionList.setSelectedIndex(aux);
            selectedid = aux;
        }
        lbCounterEasy.setText(nQ[0] + "/" + totalQuestions[0]);
        lbCounterMedium.setText(nQ[1] + "/" + totalQuestions[1]);
        lbCounterHard.setText(nQ[2] + "/" + totalQuestions[2]);
    }

    private void updateActualQuestion() {
        if (questions.isEmpty() || selectedQuizzId < 0 || selectedQuizzId > questions.size())
        {
            taQuestion.setText("");
            for (int i = 0 ; i < answers.length; i++)
                answers[i].setText("");
            rbCorrect[0].setSelected(true);
            return;
        }
        IQuestion q = questions.get(selectedQuizzId);
        rbDif[q.getDifficulty()].setSelected(true);
        
        tbName.setText(q.getName());
        taQuestion.setText(q.getText());
        
        String[] a = q.getAnswers();
        
        for (int i = 0 ; i < answers.length; i++)
        {
            answers[i].setText(a[i]);
            int x = q.getIndexCorrect();
            if (q.getIndexCorrect() == i)
                rbCorrect[i].setSelected(true);
        }
        
    }

    private boolean verificaQuestion() {
        
        if (selectedQuizzId < 0 || selectedQuizzId >= questions.size())
            return true;
        
        IQuestion question = questions.get(selectedQuizzId);
        if (question.getName().isEmpty())
        {
            JOptionPane.showMessageDialog(this, "Question name is empty", "Question name required", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        if (question.getText().isEmpty())
        {
            JOptionPane.showMessageDialog(this, "Question text is empty", "Question text required", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        String answers[] = question.getAnswers();
        if (answers[0].isEmpty() || answers[1].isEmpty() || answers[2].isEmpty() || answers[3].isEmpty())
        {
            JOptionPane.showMessageDialog(this, "You must fulfill all Answers", "All answers required", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        return true;
    }
    
    private boolean verificaQuestionComponents() {
        if (tbName.getText().isEmpty())
        {        
            JOptionPane.showMessageDialog(this, "Question name is empty", "Question name required", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        if (taQuestion.getText().isEmpty())
        {
            JOptionPane.showMessageDialog(this, "Question text is empty", "Question text required", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        if (textAnswerA.getText().isEmpty() || textAnswerB.getText().isEmpty() || textAnswerC.getText().isEmpty() || textAnswerD.getText().isEmpty())
        {
            JOptionPane.showMessageDialog(this, "You must fulfill all Answers", "All answers required", JOptionPane.ERROR_MESSAGE);
            return false;
        }
        
        return true;
    }

    private void setQuestionComponents(boolean enabled) {
        rbAnswerA.setEnabled(enabled);
        rbAnswerB.setEnabled(enabled);
        rbAnswerC.setEnabled(enabled);
        rbAnswerD.setEnabled(enabled);
        
        rbEasy.setEnabled(enabled);
        rbMedium.setEnabled(enabled);
        rbHard.setEnabled(enabled);
        
        tbName.setEnabled(enabled);
        
        taQuestion.setEnabled(enabled);
    }
}
